/*River Warren
 *October 7, 2011
 *Updated Nov 5, 2011 - River Warren
 *Updated Nov 16, 2011 - Devin Manno
 *
 *Room.cpp
 */

#include "Room.h"
#include "Utility.h"
#include <strings.h>
// Define directions in the array (this is way too hard codded in my opinion but so be it)
#define north 0
#define east 1
#define south 2
#define west 3

//constructor sets all values to null
Room::Room()
{
    setObjectType("Room");
    directions[north].setDirectionName("north");
    directions[south].setDirectionName("south");
    directions[east].setDirectionName("east");
    directions[west].setDirectionName("west");
    visited = false;
}

Room::~Room()
{
}


void Room::setLinkID(string linkIDToSet) {

    linkID = Utility::removeGarbage(linkIDToSet);
}

void Room::setObjectID(string objectIDToSet) {

    objectID = Utility::removeGarbage(objectIDToSet);
}

//writes new room state to file
void Room::saveRoom(string dir)
{
    //file name is Id and extention
    string fileName = dir + '/' + linkID + ".grm";
    ofstream out(fileName.c_str());
    try
    {
        if(out.is_open())
        {

            out << linkID << ',' << visited << endl
                                 << getDesc() << endl
                                 << directions[north] << endl
                                 << directions[east] << endl
                                 << directions[south] << endl
                                 << directions[west] << endl;
            RoomInventory::saveRoomInventory(out);

        }
        else
        {
            string errorMsg = "Error: File " + fileName + " did not open.";
            throw errorMsg;
        }
    }
    catch(exception &e)
    {
        string errorMsg = "Error: File " + fileName + " did not open.";
        throw errorMsg;
    }
    out.close();
}


//should be put into try catch block
//	if file does not exist (bad ID) throw bad ObjectID error
//	loads new room from file
//	parameter: ID of new room
void Room::loadRoom(string newID)
{
    char data[MAX] = ""; //to hold line
    char *tk = NULL;
    string fileName;//string to hold file name
    fileName = newID + ".grm";//file name is room Id (obtained from this room link) and extention
    ifstream in(fileName.c_str());//opwn file
    if(in)
    {
        in.getline(data,MAX - 1, '\n'); //get line

        tk = strtok(data, DELIMS); // get next token
        setLinkID(tk);//first token is room Id
        setObjectID(tk); // set object ID
        tk = strtok(NULL, DELIMS); // get next token
        visited = atoi(tk);
        in.getline(data,MAX - 1, '\n'); //get line
        setDesc(data);
        in >> directions[north]
           >> directions[east]
           >> directions[south]
           >> directions[west];
        RoomInventory::loadRoomInventory(in);
    }
    else
    {
        string errorMsg = "Bad ObjectID error";
        throw errorMsg;
    }
    in.close();
}

Direction * Room::getDirections() {

    Direction * dirPTR;

    dirPTR = directions;

    return dirPTR;
}

Direction * Room::getDirection(std::string directionToGet) {

    if (directionToGet == "north") {
        return &directions[north];
    }
    if (directionToGet == "east") {
        return &directions[east];
    }
    if (directionToGet == "west") {
        return &directions[west];
    }
    if (directionToGet == "south") {
        return &directions[south];
    }
    else {
        Direction * badDirection = new Direction("Bad Direction");

        return badDirection;
    }

}

const bool Room::getVisited(){
	return visited;
}

void Room::setVisited(){
	visited = true;
}

const string Room::getObjectID() {

    return objectID;
}
//********************************************************
// Overloaded == operator. Gives ability to "search" class  *
// *******************************************************

bool operator== (const Room &obj1, const string &objectID) {

    return (obj1.objectID == objectID);
}

//********************************************************
// Overloaded == operator. Gives ability to "search" class  *
// *******************************************************

bool operator== (const Room &obj1, const Room &obj2) {

    return (obj1.objectID == obj2.objectID);
}

